package com.mutouren.modules.datapush.strategy;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.mutouren.common.log.LogAlias;
import com.mutouren.common.log.LogManager;
import com.mutouren.common.log.Logger;
import com.mutouren.modules.datapush.base.PushMessage;
import com.mutouren.modules.datapush.base.PushStatus;

public class RepeatStrategyManager {
	private Map<String, RepeatStrategy> mapRepeatStrategy = new HashMap<String, RepeatStrategy>();
	
	private final static Logger errorLogger = LogManager.getLogger(LogAlias.SystemError.name());
	public RepeatStrategyManager() {};
	
	public RepeatStrategy getRepeatStrategy(String methodAlias) {
		return mapRepeatStrategy.get(methodAlias);
	}
	
	public boolean isExpired(String methodAlias, PushMessage message) {
		if (message.getRequestCount() == 0) return false;
		
		RepeatStrategy strategy = this.getRepeatStrategy(methodAlias);
		if (strategy == null) {
			errorLogger.info("logic maybe error: strategy == null, is RepeatStrategy update?");
			return true; 	
		}
		
		if (message.getRequestCount() >= strategy.getMaxRequestCount()) {
			errorLogger.info("logic maybe error: getRequestCount() >=getMaxFailCount(), is RepeatStrategy update?");
			return true; 
		}
		
		// for debug 
		//Date expiredData = DateUtils.addMinutes(message.getCreateTime(), strategy.getHourLimit());		
		Date expiredData = DateUtils.addHours(message.getCreateTime(), strategy.getHourLimit());
		
		return new Date().getTime() - expiredData.getTime() > 0;
	}
	
	public boolean isPermitSend(String methodAlias, PushMessage message) {
		if (message.getRequestCount() == 0) return true;
		
		RepeatStrategy strategy = this.getRepeatStrategy(methodAlias);
		if (strategy == null) return false;
		
		int minuteInterval = (int)Math.pow(strategy.getPower(), message.getRequestCount()); 
		if (minuteInterval > strategy.getMaxHourInterval() * 60) {
			minuteInterval = strategy.getMaxHourInterval() * 60;
		}
		
		// for debug
		//return new Date().getTime() - DateUtils.addSeconds(message.getLastUpdateTime(), minuteInterval).getTime() > 0;
		return new Date().getTime() - DateUtils.addMinutes(message.getLastUpdateTime(), minuteInterval).getTime() > 0;
	}
	
	public boolean checkSuccessFlag(String methodAlias, String value) {
		
		if (StringUtils.isBlank(methodAlias)) return true;
		RepeatStrategy strategy = this.getRepeatStrategy(methodAlias);
		if (strategy == null) return true;
		if (StringUtils.isBlank(strategy.getSuccessFlag())) return true;
		
		//? later, support regular expressions
		
		return (strategy.getSuccessFlag().equals(value));		
	}
	
	public boolean isPermitRepeat(String methodAlias, PushMessage message) {
		if (message.getPushStatus() == PushStatus.SUCCESS) return false;
		
		RepeatStrategy strategy = this.getRepeatStrategy(methodAlias);
		if (strategy == null) return false;
		
		return strategy.getMaxRequestCount() - message.getRequestCount() > 0;
	}	
	
	public void addRepeatStrategy(RepeatStrategy strategy) {
		mapRepeatStrategy.put(strategy.getMethodAlias(), strategy);
	}	
}
